#include "mesinkata.h"

boolean IsCCIgnored() {
	return (CC == BLANK || CC == NEWLINE || CC == ',');
}

void Ignore_Blank(FILE *f) {
	while ((CC == BLANK || CC == NEWLINE) && CC != MARK) ADV(f);
}

void StartString(FILE *f) {
	START(f);
	end = false;
	Ignore_Blank(f);
	if (CC != MARK)
		CopyString(f);
	else {
		end = true;
		//ADV(f);
	}
}

void AdvString(FILE *f) {
	Ignore_Blank(f);
	if (CC != MARK)
		CopyString(f);
	else {
		end = true;
		//ADV(f);
	}
}

void CopyString(FILE *f) {
	end = false;
	int i = 0;
	
	while ((CC != MARK && CC != NEWLINE) && CC != BLANK) {
		cString.s[i] =  CC;
		i++;
		ADV(f);
	}
	
	cString.length = i;
	cString.s[i] = '\0';
}

boolean IsStringEqual(string k1 , string k2) {
	int i = 0;
	
	if (k1.length != k2.length)
		return false;
	else {
		while (i < k1.length && k1.s[i] == k2.s[i])
			i++;
		
		if (i == k1.length)
			return true;
		else
			return false;
	}
}

string StrCat(string s1 , string s2) {
	string s = s1;
	int i = s.length , j = 0;
	
	while (j < s2.length) {
		s.s[i] = s2.s[j];
		i++;
		j++;
	}
	s.length = i;
	s.s[i] = '\0';
	
	return s;
}

boolean IsStringLt(string s1 , string s2) {
	boolean retval;
	int i;
	
	i = 0;
	while (i < s2.length && i < s1.length && s1.s[i] == s2.s[i])
		i++;
	if (s1.s[i] < s2.s[i])
		retval = true;
	else
	if (s1.s[i] > s2.s[i])
		retval = false;
	else
		retval = s1.length < s2.length;
	
	return retval;
}

string CharToString(char *c) {
	int i = 0;
	string s;
	
	while (c[i] != '\0') {
		s.s[i] = c[i];
		i++;
	}

	s.length = i;
	s.s[i] = '\0';
	
	return s;
}

char* StringToChar(string s) {
	char *t = (char *)malloc(sizeof(char) * s.length);

	sprintf(t , "%s" , s.s);
	
	return t;
}

int StringToInt(string s) {
	int retval = 0 , i = 0 , minus = 1;
	
	if (!IsStringEqual(s , CharToString("NULL"))) {
		while (i < s.length) {
			if (s.s[i] >= '0' && s.s[i] <= '9' && retval != -999999999) {
				retval = retval*10 + (s.s[i] - '0');
			}
			else if (s.s[i] == '-')
				minus = -1;
			else if (s.s[i] != BLANK) {
				retval = -999999999;
				minus = 1;
			}
			i++;
		}
		
		return retval * minus;
	}
	else
		return -1;
}

string IntToString(long x) {
	string s;
	int  i = 0;    
	sprintf(s.s , "%ld" , x);
	
	while (s.s[i] != '\0') i++;
	s.length = i;
	
	return s;
}

float StringToFloat(string s) {
	int i = 0;
	float r = 0 , mul = 1;
	boolean minus = 1;
	
	if (!IsStringEqual(s , CharToString("NULL"))) {
		while (s.s[i] != '.' && i < s.length) {
			if (s.s[i] != '-') 
				r = r*10 + (s.s[i] - '0');
				
			if (s.s[i] == '-' && i == 0)
				 minus = -1;
			
			i++;
		}
		
		if (s.s[i] == '.') {
			i++;
			while (i < s.length) {
				mul *= 0.1;
				r = r + mul * (s.s[i] - '0'); 
				i++;
			}
		}
		else {
			
		}
		
		return r * minus;
	}
	else
		return -1;
}

string FloatToString(float x) {
	string s;
	boolean isFloat = false;
	int  i = 0 , pos1 = 0;
	sprintf(s.s , "%0.6f" , x);
	
	while (s.s[i] != '\0') {
		i++;
		if (s.s[i] == '.') {
			isFloat = true;
		}
		
		if (isFloat && s.s[i] != '0' && pos1 == 0 && s.s[i] != '.')
			pos1 = i;
	}
	
	if (pos1 <= 6 && pos1 != 0) {
		s.length = i;
		s.s[i] = '\0';
	}
	else {
		sprintf(s.s , "%d" , (int)x);
		i = 0;
		while (s.s[i] != '\0')
			i++;
			
		s.length = i;
		s.s[i] = '\0';
	}
	
	return s;
}

boolean StringToBool(string s) {
	return IsStringEqual(s , CharToString("TRUE"));
}

string BoolToString(boolean x) {
	if (x)
		return CharToString("TRUE");
	else
		return CharToString("FALSE");
}


string SubString(string s, int start, int length){
	string retval;
	int i;
	
	for (i = 0;i < length;i++){
		retval.s[i] = s.s[i+start];
	}
	retval.s[i] = '\0';
	retval.length = i;
	
	return retval;
}

void ToUpperCase(string *s)
{
	int i;
	for(i = 0; s->s[i]!='\0'; i++) {
		if(s->s[i] >= 97 && s->s[i] <=122) s->s[i] -= 32;
	}
}

string UpperCase(string s) {
	string S = s;
	int i;
	for(i = 0; S.s[i]!='\0'; i++) {
		if(S.s[i] >= 97 && S.s[i] <=122) S.s[i] -= 32;
	}
	return S;
}

string LowerCase(string s) {
	string S = s;
	int i;
	for(i = 0; S.s[i]!='\0'; i++) {
		if(S.s[i] >= 65 && S.s[i] <=90) S.s[i] += 32;
	}
	return S;
}

string TrimWhiteSpace(string s) {
	string S;
	int i , j;
	j = 0;
	
	for(i=0 ; i<=s.length ; i++) {
		if (s.s[i] != ' ') {
			S.s[j] = s.s[i];
			j++;
		}
	}
	S = CharToString(S.s);
	return S;
}

string DeleteSubString(string s , int pos , int length) {
	return StrCat(SubString(s , 0 , pos) , SubString(s , pos + length , s.length - (pos + length)));
}
